---
import { render, type CollectionEntry } from 'astro:content'

import { getBlogCollection, sortMDByDate } from 'astro-pure/server'
import PostLayout from '@/layouts/BlogPost.astro'

export const prerender = true

export async function getStaticPaths() {
  const posts = sortMDByDate(await getBlogCollection())
  // 添加检查确保posts存在且不为空
  if (!posts || posts.length === 0) {
    return []
  }
  return posts.map((post) => ({
    params: { id: post.id },
    props: { post, posts }
  }))
}

type Props = {
  post: CollectionEntry<'blog'>
  posts: CollectionEntry<'blog'>[]
}

const { post, posts } = Astro.props
const { Content, headings, remarkPluginFrontmatter } = await render(post)
---

<PostLayout {post} {posts} {headings} {remarkPluginFrontmatter}>
  <Content />
</PostLayout>